package com.shopping.control;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.shopping.dao.impl.BizDaoImpl;
import com.shopping.entity.TbManager;
import com.shopping.service.impl.BizServiceImpl;

/**
 * Servlet implementation class bg_landServlet
 */
public class bg_landServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public bg_landServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see Servlet#init(ServletConfig)
     */
    public void init(ServletConfig config) throws ServletException {
        // TODO Auto-generated method stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doPost(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
            IOException {
        // TODO Auto-generated method stub
        response.setCharacterEncoding("gbk");
        response.setContentType("text/html");

        BizDaoImpl bizdao = new BizDaoImpl();
        BizServiceImpl bsi = new BizServiceImpl();
        bsi.setBizdao(bizdao);

        String account = request.getParameter("account"); // 用户名
        String password = request.getParameter("password");// 密码

        HttpSession session = request.getSession();
        session.setAttribute("password", password);
        session.setAttribute("account", account);
        PrintWriter out = response.getWriter();
        if (account.equals("") || password.equals("")) {
            out.print("<script>");
            out.print("alert('表单信息不能为空!');");
            out.print("window.location.href='bg-land.jsp';");
            out.print("</script>");
            out.flush();
            out.close();
            return;
        }

        boolean flag = bsi.tb_manager(account, password);
        if (flag) {
            TbManager tbManager = new TbManager();
            tbManager.setAccount(account);
            TbManager tb = bsi.getoneManager(tbManager);
            session.setAttribute("TM", tb);
            // 订单详情
            Date dNow = new Date();
            SimpleDateFormat ft = new SimpleDateFormat("yyyy年MM月dd日 E ");
            session.setAttribute("date", ft.format(dNow));
            // request.getRequestDispatcher("order").forward(request, response);
            out.print("<script>");
            out.print("alert('登陆成功!');");
            out.print("window.location.href='order';");
            out.print("</script>");
            out.flush();
            out.close();

        } else {
            out.print("<script>");
            out.print("alert('用户名或密码错误!');");
            out.print("window.location.href='bg-land.jsp';");
            out.print("</script>");
            out.flush();
            out.close();
        }

    }
}
